namespace SubSonic
{
    using SubSonic.Utilities;
    using System;

    [Serializable]
    public class OrderBy
    {
        private string orderString;

        private OrderBy()
        {
        }

        public static OrderBy Asc(TableSchema.TableColumn col)
        {
            OrderBy by = new OrderBy();
            by.orderString = " ORDER BY " + Utility.QualifyColumnName(col.Table.Name, col.ColumnName, col.Table.Provider) + " ASC";
            return by;
        }

        public static OrderBy Asc(string columnName)
        {
            OrderBy by = new OrderBy();
            by.orderString = " ORDER BY [" + columnName + "] ASC";
            return by;
        }

        public static OrderBy Asc(TableSchema.TableColumn col, string tableAlias)
        {
            OrderBy by = new OrderBy();
            by.orderString = " ORDER BY " + Utility.QualifyColumnName(tableAlias, col.ColumnName, col.Table.Provider) + " ASC";
            return by;
        }

        public static OrderBy Desc(TableSchema.TableColumn col)
        {
            OrderBy by = new OrderBy();
            by.orderString = " ORDER BY " + Utility.QualifyColumnName(col.Table.Name, col.ColumnName, col.Table.Provider) + " DESC";
            return by;
        }

        public static OrderBy Desc(string columnName)
        {
            OrderBy by = new OrderBy();
            by.orderString = " ORDER BY [" + columnName + "] DESC";
            return by;
        }

        public static OrderBy Desc(TableSchema.TableColumn col, string tableAlias)
        {
            OrderBy by = new OrderBy();
            by.orderString = " ORDER BY " + Utility.QualifyColumnName(tableAlias, col.ColumnName, col.Table.Provider) + " DESC";
            return by;
        }

        public static OrderBy PassedValue(string orderByValue)
        {
            OrderBy by = new OrderBy();
            by.orderString = " ORDER BY " + orderByValue;
            return by;
        }

        public void TrimDirective()
        {
            if (!string.IsNullOrEmpty(this.orderString))
            {
                this.orderString = this.orderString.Replace(" ORDER BY ", string.Empty).Trim();
            }
        }

        public string OrderString
        {
            get
            {
                return this.orderString;
            }
            set
            {
                this.orderString = value;
            }
        }
    }
}

